1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| #include<stdio.h>
#define N 11
int search(int dist1[N][N],int vertex[N]) { int queue[20]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; int flag[N]={0}; int front=0; int rear=0; int temp=0; int i; queue[rear]=0; flag[0]=1; rear++; while(queue[front]!=-1) { temp=queue[front]; for(i=0;i<N;i++) if(dist1[temp][i]!=0&&flag[i]==0) { queue[rear++]=i; flag[i]=1; vertex[i]=temp; if(i==10) return 1; } front++; if(front==N) break; } if(queue[rear-1]!=10) return 0; return 1; }
int modify(int dist[N][N],int dist1[N][N],int vertex[N],int s,int t,int flow) { int i,j; int min=10000;
i=vertex[t]; j=t; while(j!=s) { if(dist1[i][j]<min) min=dist1[i][j]; j=i; i=vertex[i]; } i=vertex[t]; j=t; flow=min;
while(j!=s) { if(dist1[i][j]>0) dist1[j][i]=dist1[i][j]; dist1[i][j]=dist1[i][j]-min; dist[i][j]=dist[i][j]+min-dist[j][i]; if(dist[i][j]<0) { dist[j][i]=-dist[i][j]; dist[i][j]=0; } j=i; i=vertex[i]; } printf("原始流网络矩阵:\n"); for(int i=0;i<N;i++) { for(int j=0;j<N;j++) printf("%d ",dist[i][j]); printf("\n"); }
printf("残留网络矩阵:\n"); for(int i=0;i<N;i++) { for(int j=0;j<N;j++) printf("%d ",dist1[i][j]); printf("\n"); }
return flow; }
void FordFulkerson(int dist[N][N],int dist1[N][N]) { int vertex[N]={0}; int flow=0; while(search(dist1,vertex)==1) { flow=flow+modify(dist,dist1,vertex,0,10,0); printf("\n"); } printf("最大流为%d \n",flow);
}
void main() { int dist1[N][N]={{0,1,1,1,1,1,0,0,0,0,0}, {0,0,0,0,0,0,1,0,0,0,0}, {0,0,0,0,0,0,1,0,1,0,0}, {0,0,0,0,0,0,0,1,1,1,0}, {0,0,0,0,0,0,0,0,1,0,0}, {0,0,0,0,0,0,0,0,1,0,0}, {0,0,0,0,0,0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0}}; int dist[N][N]={0};
FordFulkerson(dist,dist1);
}
|